class UserTripsController < ApplicationController
  # GET /user_trips
  # GET /user_trips.xml
  def index
    @user_trips = UserTrip.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @user_trips }
    end
  end

  # GET /user_trips/1
  # GET /user_trips/1.xml
  def show
    @user_trip = UserTrip.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @user_trip }
    end
  end

  # GET /user_trips/new
  # GET /user_trips/new.xml
  def new
    @user_trip = UserTrip.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @user_trip }
    end
  end

  # GET /user_trips/1/edit
  def edit
    @user_trip = UserTrip.find(params[:id])
  end

  # POST /user_trips
  # POST /user_trips.xml
  def create	
	@user_trip = UserTrip.new()
	@user_trip.user_id = params[:user_id]
	@user_trip.trip_id = params[:trip_id]
	respond_to do |format|
	  if @user_trip.save
		notification(User.find_by_id(Trip.find_by_id(params[:trip_id]).user_id),User.find_by_id(params[:user_id]))
		flash[:notice] = 'Amigo agregado con exito.'
		format.html { redirect_to :controller => 'users', :action => 'home', :id => current_user.id }
		format.xml  { render :xml => @user_trip, :status => :created, :location => @user_trip }
	  else
		flash[:notice] = 'Oopss tu amigo no pudo ser agregado, intenta de nuevo.'
		format.html { redirect_to :controller => 'users', :action => 'home', :id => current_user.id }
		format.xml  { render :xml => @user_trip.errors, :status => :unprocessable_entity }
	  end
	end
  end

  # PUT /user_trips/1
  # PUT /user_trips/1.xml
  def update
    @user_trip = UserTrip.find(params[:id])

    respond_to do |format|
      if @user_trip.update_attributes(params[:user_trip])
        flash[:notice] = 'UserTrip was successfully updated.'
        format.html { redirect_to(@user_trip) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @user_trip.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /user_trips/1
  # DELETE /user_trips/1.xml
  def destroy
    @user_trip = UserTrip.find(params[:id])
    @user_trip.destroy

    respond_to do |format|
      format.html { redirect_to(user_trips_url) }
      format.xml  { head :ok }
    end
  end
  
	def notification (sender,recipient)
		title = sender.name+" "+sender.lastname+" te ha invitado a uno de sus viajes!"
		message = "Este correo es para informarte que "+sender.name+" "+sender.lastname+" te ha invitado
		a un viaje. Si no deseas participar en dicho viaje dirigete a tu perfil y eliminalo de tu lista de viajes."
		Emailer.deliver_notification(title,message,recipient)	
	end
	
	def leave_trip
		@user_trip = UserTrip.find_by_trip_id_and_user_id(params[:id],current_user.id)
		
		@user_trip.destroy
		
		respond_to do |format|
		  format.html { redirect_to :controller => 'users', :action => 'home', :id => current_user.id }
		  format.xml  { head :ok }
		end
	end
end
